﻿using eShop.Core;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace eShop.RDBSStrategy.SqlServer
{
    /// <summary>
    /// 客户信息
    /// </summary>
    public partial class RDBSStrategy : IRDBSStrategy
    {

        #region 客户信息
        /// <summary>
        /// 创建客户
        /// </summary>
        /// <param name="_customerInfo"></param>
        /// <returns></returns>
        public int CreateCustomer(CustomerInfo _customerInfo)
        {
            string commandText = string.Format(@"insert into {0}customer(encode,fullname,shortname,custindustryid,custtypeid,
                                                                        custlevelid,custdegreeid,regionid,contact,mobile,tel,fax,email,
                                                                        companyaddress,companysite,traceusername,description)
            values('{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}')",
            RDBSHelper.RDBSTablePre,
            _customerInfo.Encode, _customerInfo.Fullname, _customerInfo.Shortname, _customerInfo.Custindustryid, _customerInfo.Custtypeid,
            _customerInfo.Custlevelid, _customerInfo.Custdegreeid, _customerInfo.Regionid, _customerInfo.Contact, _customerInfo.Mobile, _customerInfo.Tel, _customerInfo.Fax, _customerInfo.Email,
            _customerInfo.Companyaddress, _customerInfo.Companysite, _customerInfo.Traceusername, _customerInfo.Description);
            return RDBSHelper.ExecuteNonQuery(commandText);
        }
        /// <summary>
        /// 更新客户
        /// </summary>
        /// <param name="_customerInfo"></param>
        public void UpdateCustomer(CustomerInfo _customerInfo)
        {
            string commandText = string.Format(@"update {0}customer 
            set encode ='{1}',fullname = '{2}',shortname = '{3}',custindustryid='{4}',
                custtypeid='{5}',custlevelid='{6}',custdegreeid='{7}',regionid='{8}',
                contact='{9}',mobile='{10}',tel='{11}',fax='{12}',
                email='{13}',companyaddress='{14}',companysite='{15}',traceusername='{16}',description='{17}'
                where customerid = {18}",
                RDBSHelper.RDBSTablePre, _customerInfo.Encode, _customerInfo.Fullname, _customerInfo.Shortname, _customerInfo.Custindustryid,
                _customerInfo.Custtypeid, _customerInfo.Custlevelid, _customerInfo.Custdegreeid, _customerInfo.Regionid,
                _customerInfo.Contact, _customerInfo.Mobile, _customerInfo.Tel, _customerInfo.Fax,
                _customerInfo.Email, _customerInfo.Companyaddress, _customerInfo.Companysite, _customerInfo.Traceusername, _customerInfo.Description,
                _customerInfo.Customerid);
            RDBSHelper.ExecuteNonQuery(commandText);
        }
        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="wid"></param>
        /// <returns></returns>
        public int DeleteCustomerById(int cid)
        {
            //判断是否使用
            string commandText = string.Format("select count(1) from {0}users where customerid={1}", RDBSHelper.RDBSTablePre,cid);
            object oret = RDBSHelper.ExecuteScalar(CommandType.Text, commandText);
            int ret = TypeHelper.ObjectToInt(oret);
            if (ret > 0)
            {
                return -1;
            }
            else
            {
                commandText = string.Format("delete from {0}customer where customerid={1}", RDBSHelper.RDBSTablePre, cid);
                return RDBSHelper.ExecuteNonQuery(commandText);
            }
        }
        /// <summary>
        /// 得到客户数据列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetCustomerList()
        {
            string commandText = string.Format("select * from {0}customer", RDBSHelper.RDBSTablePre);
            return RDBSHelper.ExecuteDataset(CommandType.Text, commandText).Tables[0];
        }
        #endregion

    }
}
